/**
 * 
 */

package CCT220_Aula15_Exercicio01_13106;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Aula15_Exercicio01_13106
 * </br>Class: ControleDisciplina
 * </br>Last Change: 05/10/2011 20:17:51
 * </br>Description: Controler for Disciplina
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class ControleDisciplina {
	private final String arquivo         = "disc.dat";
	private Vector       listaDisciplina = new Vector();


	// Método de inserção
	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula15_Exercicio01_13106
	 * </br>Class: ControleDisciplina
	 * </br>Method: insereDisciplina
	 * </br>Last Change: 05/10/2011 20:18:47
	 * </br>Description: Insert a Disciplina in listaDisciplina
	 * </br>******************************</br>
	 * 
	 * @param pCodigo
	 * @param pNome
	 * @param pCargaHoraria
	 * @throws Exception
	 */
	public void insereDisciplina(int pCodigo, String pNome, int pCargaHoraria)
	        throws Exception {

		Disciplina objDisc = new Disciplina(pCodigo, pNome, pCargaHoraria);
		listaDisciplina.add(objDisc);
		gravaDisciplinas();
	}


	// Método de composição dos dados da disciplina utilizado para visualização
	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula15_Exercicio01_13106
	 * </br>Class: ControleDisciplina
	 * </br>Method: getDisciplina
	 * </br>Last Change: 05/10/2011 20:18:51
	 * </br>Description: Get a Disciplina's description
	 * </br>******************************</br>
	 * 
	 * @param objPDisciplina
	 * @return Disciplina's description
	 */
	private String getDisciplina(Disciplina objPDisciplina) {

		return "Código: " + objPDisciplina.getCodigo() + " Nome: "
		        + objPDisciplina.getNome() + " Carga Horária: "
		        + objPDisciplina.getCargaHoraria() + "\n";
	}


	// Método para obtenção das lista de disciplinas
	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula15_Exercicio01_13106
	 * </br>Class: ControleDisciplina
	 * </br>Method: getListaDisciplinas
	 * </br>Last Change: 05/10/2011 20:18:55
	 * </br>Description: Get the description of all disciplines
	 * </br>******************************</br>
	 * 
	 * @return Description of all Disciplinas
	 */
	public String getListaDisciplinas() {

		String result = "";
		Disciplina objDisciplina = null;
		for (int intIdx = 0; intIdx < listaDisciplina.size(); intIdx++) {
			objDisciplina = (Disciplina) listaDisciplina.elementAt(intIdx);
			result += getDisciplina(objDisciplina);
		}
		if (result.equalsIgnoreCase("")) {
			return "Não existem disciplinas cadastradas.";
		} else {
			return result;
		}
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula15_Exercicio01_13106
	 * </br>Class: ControleDisciplina
	 * </br>Method: getDisciplina
	 * </br>Last Change: 05/10/2011 20:18:58
	 * </br>Description: Search for a Disciplina and return its description
	 * </br>******************************</br>
	 * 
	 * @param pCodigo
	 * @return A Disciplina description
	 */
	public String getDisciplina(int pCodigo) {

		Disciplina objDisciplina = null;
		for (int intIdx = 0; intIdx < listaDisciplina.size(); intIdx++) {
			objDisciplina = (Disciplina) listaDisciplina.elementAt(intIdx);
			if (objDisciplina.getCodigo() == pCodigo) { return getDisciplina(objDisciplina); }
		}
		return "Não foi encontrada nenhuma disciplina com o código " + pCodigo
		        + ".";
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula15_Exercicio01_13106
	 * </br>Class: ControleDisciplina
	 * </br>Method: gravaDisciplinas
	 * </br>Last Change: 05/10/2011 20:19:02
	 * </br>Description: Persist the listaDisciplina to a file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	public void gravaDisciplinas() throws Exception {

		FileOutputStream objFileOS = new FileOutputStream(arquivo);
		ObjectOutputStream objOS = new ObjectOutputStream(objFileOS);
		objOS.writeObject(listaDisciplina);
		objOS.flush();
		objOS.close();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula15_Exercicio01_13106
	 * </br>Class: ControleDisciplina
	 * </br>Method: recuperaDisciplinas
	 * </br>Last Change: 05/10/2011 20:19:37
	 * </br>Description: Recover the list of Disciplinas from a file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	public void recuperaDisciplinas() throws Exception {

		File objFile = new File(arquivo);
		if (objFile.exists()) {
			FileInputStream objFileIS = new FileInputStream(arquivo);
			ObjectInputStream objIS = new ObjectInputStream(objFileIS);
			listaDisciplina = (Vector) objIS.readObject();
			objIS.close();
		}
	}
}
